package com.gperp.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gperp.common.GPResult;
import com.gperp.mapper.AsRolePremissionMapper;
import com.gperp.pojo.AsRolePremission;
import com.gperp.pojo.AsRolePremissionExample;
import com.gperp.service.PremissionService;

@Service
public class PremissionServiceImpl implements PremissionService {
	@Resource
	private AsRolePremissionMapper premissionMapper;
	
	@Override
	public List<AsRolePremission> queryList(Long id) {
		AsRolePremissionExample example =new AsRolePremissionExample();
		example.createCriteria().andRoleidEqualTo(id);
		return premissionMapper.selectByExample(example);
	}

	@Override
	public GPResult updatePremission(Long roleid, String add,String del) {
		if(roleid==null){
			return GPResult.build(200, "操作失败");
		}else{
			if(add.equals("")&&del.equals("")){
				return GPResult.build(200, "操作失败");
			}else{
				try {
					if(!add.equals("")){
						String[] str = add.split(",");
						for (String string : str) {
							AsRolePremission premiss = new AsRolePremission();
							premiss.setIsstart(1);
							premiss.setRoleid(roleid);
							premiss.setFunctionid(Long.parseLong(string));
							premiss.setCreationtime(new Date());
							premiss.setCreatedby("admin");
							premissionMapper.insertSelective(premiss);
						}
					}
					if(!del.equals("")){
						String[] str = del.split(",");
						for (String string : str) {
							AsRolePremissionExample example = new AsRolePremissionExample();
							example.createCriteria().andRoleidEqualTo(roleid).andFunctionidEqualTo(Long.parseLong(string));
							premissionMapper.deleteByExample(example);
						}
					}
				} catch (NumberFormatException e) {
					e.printStackTrace();
					return GPResult.build(200, "操作失败");
				}
			}
		}
		return GPResult.ok();
	}

}
